-- UIHugeHole
-- Created by chengb Aug/4/2015
-- 巨坑界面

UIHugeHole = class("UIHugeHole", function()
    return cc.Layer:create();
end);

function UIHugeHole.create(gridData)
    return UIHugeHole.new(gridData);
end

-- 材料列表（策划要求不管要不要消耗，全部都要显示出来，那只好写死了）
local MATERIALS = { 7024, 7023, 7022, };

-- 贤者冈布奥的编号
local PRIEST_PET_ID = 49;

-- 构造函数
function UIHugeHole:ctor(gridData)
    -- 初始化
    self:setName("UIHugeHole");
    local node = cc.CSLoader:createNode("layout/lab/DungeonLab.csb");
    self:addChild(node);
    self.node = node;

    self.gridData = gridData;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 注册事件处理回调函数
    self:registerEventCallback();

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 注册事件回调处理函数
function UIHugeHole:registerEventCallback()

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIHugeHole 界面析构清理");

            -- 移除所有事件关注
            EventMgr.removeAll("UIHugeHole");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end

        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end

-- 注册点击事件
function UIHugeHole:registerTouchEvent()
    -- 注册返回按钮点击事件
    local node = self.node;
    local btnBack = findChildByName(node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIHugeHole");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_click");

            self:tryBury();
        end
    end
    btnOk:addTouchEventListener(onOkClick);
end

-- 尝试埋葬
function UIHugeHole:tryBury()
    -- 检查消耗
    local cost = FormulaM.invoke("CALC_HUGE_HOLE_COST");
    for _, arr in ipairs(cost) do
        if ItemM.getAmount(ME.user, arr[2]) < arr[3] then
            -- 材料不足
            alert(getLocStr("huge_hole_material_insufficient"));
            return;
        end
    end

    local pos = self.gridData:getPos();

    -- 触发对话
    local function endCallback()
        local grid = UIDungeonMgr.getCurLevel().grids[pos];
        grid:buryHugeHole();
    end

    -- 关闭界面
    closeFormByName("UIHugeHole");

    -- 触发对话
    startDialogueTemplate("priest_talk2", endCallback);
end

-- 重绘
function UIHugeHole:redraw()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local btnOk      = findChildByName(self.node, "CT/btn_ok");

    -- 查询建筑配置
    local buildingInfo = BuildingBonusM.queryBuildingView(self.gridData.class);

    -- 标题
    titleLabel:setString(buildingInfo["title"]);

    -- 图片
    iconImg:loadTexture(getLevelItemIconPath(buildingInfo["icon"]));

    iconImg:setScale(1.0);
    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);

    -- 描述
    applyString(descLabel, buildingInfo["dialog"]);

    btnOk:setTitleText(buildingInfo["view_text"]);

    -- 按钮可点击
    btnOk:setTouchEnabled(true);
    btnOk:setBright(true);

    -- 重绘消耗
    self:redrawCost();
end

-- 重绘消耗
function UIHugeHole:redrawCost()
    local costPart = findChildByName(self.node, "CT/cost_part");

    -- 计算消耗
    local cost = FormulaM.invoke("CALC_HUGE_HOLE_COST");

    -- 获取需求数量
    local function getNeedAmount(classId)
        if type(cost) ~= 'table' then
            return 0;
        end

        for _, arr in pairs(cost) do
            if arr[2] == classId then
                return arr[3];
            end
        end
        return 0;
    end

    for i, id in pairs(MATERIALS) do
        local child = findChildByName(costPart, "cost_node" .. i);

        -- 显示材料图标
        local iconImg = findChildByName(child, "icon");
        local iconPath = getItemIconPath(ItemM.query(id, "icon"));
        iconImg:loadTexture(iconPath);
        iconImg:setScale(0.7);
        iconImg:setPositionY(10);

        -- 显示数量信息
        local amountLabel = findChildByName(child, "amount");
        local needAmount = getNeedAmount(id);
        local curAmount = ItemM.getAmount(ME.user, id);

        -- 消耗不足时，显示红色
        if curAmount < needAmount then
            TextStyleM.setTextStyle(amountLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_RED);
        else
            TextStyleM.setTextStyle(amountLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BEIGE);
        end

        if needAmount <= 0 then
            -- 无需消耗
            amountLabel:setString(curAmount .. "/-");
        else

            amountLabel:setString(curAmount .. "/" .. needAmount);
        end
    end
end

-- 适配
function UIHugeHole:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end





